const { getSqlLiteData } = require('../../SqlLiteDB');

module.exports = function setMatWorkProcedureRouter (router) {

    //查询物料工序列表
    router.get('/getMatWorkProcedures', async function (req, res, next) {
        try {
            let dataContent = JSON.parse(req.query['dataContent']);
            let matcode = dataContent.matcode;
            let SqlString = "SELECT * FROM matworkprocedure m JOIN workprocedure w on m.worprocedureid=w.worprocedureid " + (matcode ? ` WHERE m.matcode='${matcode}'` : '') + " order by m.matcode,m.orderindex";
            let procedures = await getSqlLiteData(SqlString);

            res.status(200).json({
                procedures,
            });
        }
        catch (ex) {
            res.status(500).json({
                result: ex,
            });
            console.log('ex :>> ', {
                result: ex,
            });
        }
    });
    //新增工序,需要先把原先的工序顺序往后移
    router.post('/updateMatWorkProcedureIndex', async function (req, res, next) {
        try {
            let dataContent = JSON.parse(req.body['dataContent']);
            let procedure = dataContent.procedure;
            //先更新后续工序的orderindex
            let updateIndexSqlString1 = `UPDATE matworkprocedure SET orderindex=orderindex+101 WHERE matcode='${procedure.matcode}' AND orderindex>='${procedure.orderindex}'`;
            let updateIndexSqlString2 = `UPDATE matworkprocedure SET orderindex=orderindex-100 WHERE matcode='${procedure.matcode}' AND orderindex>='${procedure.orderindex + 101}';`;
            let resultIndex1 = await getSqlLiteData(updateIndexSqlString1);
            let resultIndex2 = await getSqlLiteData(updateIndexSqlString2);
            //再插入新工序
            let SqlString = `REPLACE INTO matworkprocedure (matcode,orderindex,worprocedureid)
            VALUES ('${procedure.matcode}', '${procedure.orderindex}', '${procedure.worprocedureid}')`;
            let result = await getSqlLiteData(SqlString);

            res.status(200).json({
                result,
                resultIndex1,
                resultIndex2,
            });
        }
        catch (ex) {
            res.status(500).json({
                result: ex,
            });
            console.log('ex :>> ', {
                result: ex,
            });
        }
    });

    //修改工序
    router.post('/updateMatWorkProcedure', async function (req, res, next) {
        try {
            let dataContent = JSON.parse(req.body['dataContent']);
            let procedure = dataContent.procedure;
            let SqlString = `REPLACE INTO matworkprocedure (matcode,orderindex,worprocedureid)
            VALUES ('${procedure.matcode}', '${procedure.orderindex}', '${procedure.worprocedureid}')`;
            let result = await getSqlLiteData(SqlString);

            res.status(200).json({
                result,
            });
        }
        catch (ex) {
            res.status(500).json({
                result: ex,
            });
            console.log('ex :>> ', {
                result: ex,
            });
        }
    });

    //删除物料工序
    router.post('/deleteMatWorkProcedure', async function (req, res, next) {
        try {
            let dataContent = JSON.parse(req.body['dataContent']);
            let procedure = dataContent.procedure;
            //先删除工序
            let SqlString = `DELETE FROM matworkprocedure WHERE matcode='${procedure.matcode}' AND orderindex='${procedure.orderindex}'`;
            let result = await getSqlLiteData(SqlString);
            //再更新后续工序的orderindex
            let updateIndexSqlString1 = `UPDATE matworkprocedure SET orderindex=orderindex+100 WHERE matcode='${procedure.matcode}' AND orderindex>='${procedure.orderindex}'`;
            let updateIndexSqlString2 = `UPDATE matworkprocedure SET orderindex=orderindex-101 WHERE matcode='${procedure.matcode}' AND orderindex>='${procedure.orderindex + 100}';`;
            let resultIndex1 = await getSqlLiteData(updateIndexSqlString1);
            let resultIndex2 = await getSqlLiteData(updateIndexSqlString2);

            res.status(200).json({
                result,
                resultIndex1,
                resultIndex2,
            });
        }
        catch (ex) {
            res.status(500).json({
                result: ex,
            });
            console.log('ex :>> ', {
                result: ex,
            });
        }
    });

    //删除物料
    router.post('/deleteMatWorkProcedureMatcode', async function (req, res, next) {
        try {
            let dataContent = JSON.parse(req.body['dataContent']);
            let procedure = dataContent.procedure;
            //先删除工序
            let SqlString = `DELETE FROM matworkprocedure WHERE matcode='${procedure.matcode}'`;
            let result = await getSqlLiteData(SqlString);

            res.status(200).json({
                result,
            });
        }
        catch (ex) {
            res.status(500).json({
                result: ex,
            });
            console.log('ex :>> ', {
                result: ex,
            });
        }
    });
};
